﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Data.SqlClient;
using System.Net.Http;
using System.Threading;
using System.Data;
using System.Data.Odbc;
using System.Security.Cryptography;

using Microsoft.Extensions.Configuration;
using BgWorks.Enums;

namespace BgWorks.Common
{

    public class JHContext
    {
        /// <summary>
        /// 延迟加载对象
        /// </summary>
        private static readonly Lazy<JHContext> _lazy =
            new Lazy<JHContext>(() => new JHContext(), LazyThreadSafetyMode.PublicationOnly);

        /// <summary>
        /// 单例对象
        /// </summary>
        public static JHContext Instance => _lazy.Value;

        public IConfiguration Configuration { get; set; }
        public IHttpClientFactory HttpClientFactory { get; set; }

        private JHContext()
        {

        }
    }

    public static class DapperFactory
    {
        #region 变量
        private static readonly string connectionString;
        private static readonly ConnectionType _connType;


        private static IConfiguration Configuration { get; set; }



        #region 属性
        /// <summary>
        /// 连接属性oracle,odbc,mssql等，支持代码中判断不同数据的一些个性化实现  2022年4月27日
        /// 修改Conntype 为枚举  2022年5月16日
        /// </summary>
        public static ConnectionType ConnType => _connType;
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>

        static DapperFactory()
        {

            Configuration = JHContext.Instance.Configuration;
            connectionString = Configuration.GetConnectionString("JHECIS");
            Enum.TryParse(Configuration.GetSection("ConnType").Value, true, out _connType);

        }
        #endregion

        #region 方法
        /// <summary>
        /// 创建Oracle数据库连接
        ///  2022年4月27日
        /// </summary>
        /// <returns></returns>
        public static IDbConnection CreateDefaultConnection(string connKey = null, string connType = null)
        {
            if (!string.IsNullOrEmpty(connType))
            {
                return CreateOtherConnection(connKey, connType);
            }
            if (ConnType == ConnectionType.Odbc)
            {
                return CreateOdbcConnection(connKey);
            }
            else if (ConnType == ConnectionType.NpgSql)
            {
                return CreatePgSqlConnection(connKey);
            }
            else if (ConnType == ConnectionType.DmSql)
            {
                return CreateDmSqlConnection(connKey);
            }
            //else if (ConnType == ConnectionType.OracleEn)
            //{
            //    return CreateOracleEnConnection(connKey);
            //}
            else
            {
                return CreateOracleConnection(connKey);
            }
        }
        /// <summary>
        /// 创建其他业务数据库  2023-07-26
        /// </summary>
        /// <param name="connKey"></param>
        /// <param name="connType"></param>
        /// <returns></returns>
        public static IDbConnection CreateOtherConnection(string connKey = null, string connType = null)
        {
            ConnectionType type;
            Enum.TryParse(connType, out type);
            if (type == ConnectionType.Odbc)
            {
                return CreateOdbcConnection(connKey);
            }
            else if (type == ConnectionType.NpgSql)
            {
                return CreatePgSqlConnection(connKey);
            }
            else if (type == ConnectionType.DmSql)
            {
                return CreateDmSqlConnection(connKey);
            }
            //else if (type == ConnectionType.OracleEn)
            //{
            //    return CreateOracleEnConnection(connKey);
            //}
            else if (type == ConnectionType.MsSql)
            {
                return CreateMsSqlConnection(connKey);
            }
            else
            {
                return CreateOracleConnection(connKey);
            }
        }

        /// <summary>
        /// 创建ODBC数据库连接(支持gaussdb)
        ///  2022年4月27日
        /// </summary>
        /// <returns></returns>
        public static IDbConnection CreateOdbcConnection(string connKey = null)
        {
            try
            {
                //var conString = string.IsNullOrEmpty(connKey) ? connectionString : Configuration.GetConnectionString(connKey);

                //var connection = new OdbcConnection(conString);
                //connection.Open();
                //return connection;
                return null;
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error(ex);
                return null;
            }
        }

        
        /// </summary>
        /// <param name="connKey">连接key</param>
        /// <returns></returns>
        public static IDbConnection CreateOracleConnect(string conn = "")
        {
            try
            {
                var conString = string.IsNullOrEmpty(conn) ? connectionString : conn;
                var connection = new OracleConnection(conString);
                connection.Open();
                return connection;
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error(ex);
                return null;
            }
        }

        /// <summary>
        /// 创建Oracle数据库连接
        ///  2022年4月27日
        /// </summary>
        /// <param name="connKey">连接key</param>
        /// <returns></returns>
        public static IDbConnection CreateOracleConnection(string connKey = null)
        {
            try
            {
                var conString = string.IsNullOrEmpty(connKey) ? connectionString : Configuration.GetConnectionString(connKey);


                var connection = new OracleConnection(conString);
                connection.Open();
                return connection;
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error(ex);
                return null;
            }
        }

        /// <summary>
        /// 创建SqlServer数据库连接
        ///  2022年4月27日
        /// </summary>
        /// <param name="connKey">连接key</param>
        /// <returns></returns>
        public static IDbConnection CreateMsSqlConnection(string connKey)
        {
            try
            {

                //var conString = string.IsNullOrEmpty(connKey) ? connectionString : Configuration.GetConnectionString(connKey);

                //var connection = new SqlConnection(conString);
                //connection.Open();
                //return connection;
                return null;
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error(ex);
                return null;
            }
        }
        /// <summary>
        /// 创建PgSql数据库连接
        ///  2022年4月27日
        /// </summary>
        /// <param name="connKey">连接key</param>
        /// <returns></returns>
        public static IDbConnection CreatePgSqlConnection(string connKey)
        {
            try
            {

                //var conString = string.IsNullOrEmpty(connKey) ? connectionString : Configuration.GetConnectionString(connKey);

                //var connection = new NpgsqlConnection(conString);
                //connection.Open();
                //return connection;
                return null;
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error(ex);
                return null;
            }
        }

        /// <summary>
        /// 创建DMSql数据库连接 
        /// 吕珍珍 2022年9月5日
        /// </summary>
        /// <param name="connKey">连接key</param>
        /// <returns></returns>
        public static IDbConnection CreateDmSqlConnection(string connKey)
        {
            try
            {

                //var conString = string.IsNullOrEmpty(connKey) ? connectionString : Configuration.GetConnectionString(connKey);

                //var connection = new Dm.DmConnection(conString);
                //connection.Open();
                //return connection;
                return null;
            }
            catch (Exception ex)
            {
                LogHelper.GetLog().Error(ex);
                return null;
            }
        }

        ///// <summary>
        ///// 创建Oracle英文字符集数据库连接（不兼容.net core版本Devart驱动收费） 
        /////  2023年1月6日
        ///// </summary>
        ///// <param name="connKey">连接key</param>
        ///// <returns></returns>
        //public static IDbConnection CreateOracleEnConnection(string connKey)
        //{
        //    try
        //    {

        //        var conString = string.IsNullOrEmpty(connKey) ? connectionString : Configuration.GetConnectionString(connKey);

        //        var connection = new Devart.Data.Oracle.OracleConnection(conString);
        //        connection.Open();
        //        return connection;
        //    }
        //    catch (Exception ex)
        //    {
        //        LogHelper.GetLog().Error(ex);
        //        return null;
        //    }
        //}

        //public static object GetProcParameter(string DBDriver, JHOracleParameter Para)
        //{
        //    if (DBDriver == "Devart.Data.Oracle")
        //    {
        //        var outPara = new Devart.Data.Oracle.OracleParameter(Para.ParameterName, ConvertDbType(Para.OracleDbType), Para.Direction) { Value = Para.Value };
        //        if (outPara.OracleDbType != Devart.Data.Oracle.OracleDbType.Cursor)
        //        {
        //            outPara.Value = Para.Value;
        //        }
        //        return outPara;
        //    }
        //    else
        //    {
        //        var outPara = new OracleParameter(Para.ParameterName, (OracleDbType)Para.OracleDbType, Para.Direction);
        //        if (outPara.OracleDbType != OracleDbType.RefCursor)
        //        {
        //            outPara.Value = Para.Value;
        //        }
        //        return outPara;
        //    }
        //}
        ///// <summary>
        ///// Oracle英文字符集参数转换 
        ///// 张又发 2023年4月23日
        ///// </summary>
        //public static object GetProcParameter(OracleParameter Para)
        //{
        //    if (ConnType == ConnectionType.OracleEn)
        //    {
        //        var outPara = new Devart.Data.Oracle.OracleParameter(Para.ParameterName, ConvertDbType(Para.OracleDbType), Para.Direction) { Value = Para.Value };
        //        if (outPara.OracleDbType != Devart.Data.Oracle.OracleDbType.Cursor)
        //        {
        //            outPara.Value = Para.Value;
        //        }
        //        return outPara;
        //    }
        //    else
        //    {
        //        return Para;
        //    }
        //}

        //private static Devart.Data.Oracle.OracleDbType ConvertDbType(Entity.Enum.JHOracleDbType dbType)
        //{
        //    Devart.Data.Oracle.OracleDbType result;
        //    switch (dbType)
        //    {
        //        case Entity.Enum.JHOracleDbType.Blob:
        //            result = Devart.Data.Oracle.OracleDbType.Blob;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Byte:
        //            result = Devart.Data.Oracle.OracleDbType.Byte;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Clob:
        //            result = Devart.Data.Oracle.OracleDbType.Clob;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Date:
        //            result = Devart.Data.Oracle.OracleDbType.Date;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Decimal:
        //            result = Devart.Data.Oracle.OracleDbType.Double;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Double:
        //            result = Devart.Data.Oracle.OracleDbType.Double;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Long:
        //            result = Devart.Data.Oracle.OracleDbType.Long;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Int32:
        //            result = Devart.Data.Oracle.OracleDbType.Integer;
        //            break;
        //        case Entity.Enum.JHOracleDbType.NVarchar2:
        //            result = Devart.Data.Oracle.OracleDbType.NVarChar;
        //            break;
        //        case Entity.Enum.JHOracleDbType.RefCursor:
        //            result = Devart.Data.Oracle.OracleDbType.Cursor;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Varchar2:
        //            result = Devart.Data.Oracle.OracleDbType.VarChar;
        //            break;
        //        case Entity.Enum.JHOracleDbType.Boolean:
        //            result = Devart.Data.Oracle.OracleDbType.Boolean;
        //            break;
        //        case Entity.Enum.JHOracleDbType.NChar:
        //            result = Devart.Data.Oracle.OracleDbType.NChar;
        //            break;
        //        default:
        //            result = Devart.Data.Oracle.OracleDbType.VarChar;
        //            break;
        //    }
        //    return result;
        //}

        ///// <summary>
        ///// 数据类型转换
        ///// 张又发 2023年4月23日
        ///// </summary>
        //private static Devart.Data.Oracle.OracleDbType ConvertDbType(OracleDbType dbType)
        //{
        //    Devart.Data.Oracle.OracleDbType result;
        //    switch (dbType)
        //    {
        //        case OracleDbType.Blob:
        //            result = Devart.Data.Oracle.OracleDbType.Blob;
        //            break;
        //        case OracleDbType.Byte:
        //            result = Devart.Data.Oracle.OracleDbType.Byte;
        //            break;
        //        case OracleDbType.Clob:
        //            result = Devart.Data.Oracle.OracleDbType.Clob;
        //            break;
        //        case OracleDbType.Date:
        //            result = Devart.Data.Oracle.OracleDbType.Date;
        //            break;
        //        case OracleDbType.Decimal:
        //            result = Devart.Data.Oracle.OracleDbType.Double;
        //            break;
        //        case OracleDbType.Double:
        //            result = Devart.Data.Oracle.OracleDbType.Double;
        //            break;
        //        case OracleDbType.Long:
        //            result = Devart.Data.Oracle.OracleDbType.Long;
        //            break;
        //        case OracleDbType.Int32:
        //            result = Devart.Data.Oracle.OracleDbType.Integer;
        //            break;
        //        case OracleDbType.NVarchar2:
        //            result = Devart.Data.Oracle.OracleDbType.NVarChar;
        //            break;
        //        case OracleDbType.RefCursor:
        //            result = Devart.Data.Oracle.OracleDbType.Cursor;
        //            break;
        //        case OracleDbType.Varchar2:
        //            result = Devart.Data.Oracle.OracleDbType.VarChar;
        //            break;
        //        case OracleDbType.Boolean:
        //            result = Devart.Data.Oracle.OracleDbType.Boolean;
        //            break;
        //        case OracleDbType.NChar:
        //            result = Devart.Data.Oracle.OracleDbType.NChar;
        //            break;
        //        default:
        //            result = Devart.Data.Oracle.OracleDbType.VarChar;
        //            break;
        //    }
        //    return result;
        //}
        #endregion
    }
    #endregion
}